package com.files;

import java.io.*;
import java.util.ArrayList;

public class Reader {
	
	static ArrayList<String> em = new ArrayList<String>(); 
	
    public static void main(String[] arguments) {
// Read the file into an array  	
        try (
            FileReader file = new
                FileReader("tomL1016.log");
            BufferedReader buffIn = new
                BufferedReader(file)) {

        	   boolean eofIn = false;
        	   while (!eofIn) {
                   String line = buffIn.readLine();
                   if (line == null) {
                       eofIn = true;
                   } else {
                   	boolean errorMessageBegins = (line.startsWith("ERROR"));
                   		if (errorMessageBegins) {
           					em.add(line + "\n");
                   		}
                   }
            }
            buffIn.close();
        } catch (IOException e) {
            System.out.println("IOException Error -- " + e.toString());
        }
// Write to disk        
        try {
        	System.out.println(em.size());
        	FileWriter writer = new FileWriter("1016_FilteredErrorMessages.txt"); 
        	for(String buffOut: em) {
        	  writer.write(buffOut);
        	}
        	writer.close();
        	
        	} catch (IOException e) {
        	System.out.println("IOException Error -- " + e.toString());
        	} 
//        for (String eMsgs : em) {   // Useful for seeing output in console
//			System.out.println(eMsgs);}
   }
}